package com.apes.fn.fico.expense.platformFee.process;

import com.apes.fn.fico.expense.platformFee.CommonFeeProcess;
import com.apes.fn.fico.settleExpense.SettleExpense;
import com.apes.fn.fico.settleExpense.SettleExpenseRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 平台定损费用单生成平台费用细项单据
 */
@Service
public class SettleExpenseFeeProcess extends CommonFeeProcess {

    @Autowired
    SettleExpenseRepository settleExpenseRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        SettleExpense settleExpense = settleExpenseRepository.findOne(id);
        createExpense(settleExpense);
    }

    /**
     * 平台服务费
     *
     * @param settleExpense
     */
    private void createExpense(SettleExpense settleExpense) {
        if(settleExpense.getDept() == null) return;
        //卖单定损减免
        if ("in".equals(settleExpense.getType()) &&
                "platformExpense".equals(settleExpense.getExpenseType()))
            createExpenseMDDSJM(settleExpense);
        //接案介绍费
        if ("in".equals(settleExpense.getType()) &&
                "infoExpense".equals(settleExpense.getExpenseType()))
            createExpenseJAJSF(settleExpense);
    }

    /**
     * 卖单定损减免平台服务费
     *
     * @param settleExpense
     */
    private void createExpenseMDDSJM(SettleExpense settleExpense) {
        Map map = MapUtil.mapper("order", settleExpense.getId(), "orderType", "SettleExpense",
                "source", settleExpense.getOrigin(), "sourceBusinessType", settleExpense.getOriginBusinessType(),
                "summary", "", "incomeTypeId", "A09", "expenseDetailsTypeId", "002055","date",settleExpense.getCreateDate(),
                "paymentAmount", settleExpense.getLossQuotationAmt(), "expenseAmount", settleExpense.getAmount() * -1,
                "deptId", settleExpense.getDept().getId(), "companyId", settleExpense.getPayment().getId());
        createExpenseByFee(map);
    }

    /**
     * 接案介绍费平台服务费
     *
     * @param settleExpense
     */
    private void createExpenseJAJSF(SettleExpense settleExpense) {
        SettleExpense settleExpenseFee = settleExpenseRepository.findBySourceAndOriginAndDeptAndExpenseTypeAndType(settleExpense.getSource(),settleExpense.getOrigin(), settleExpense.getDept(), "platformExpense", "out");
        if(settleExpenseFee == null) return;
        Map map = MapUtil.mapper("order", settleExpense.getId(), "orderType", "SettleExpense",
                "source", settleExpense.getOrigin(), "sourceBusinessType", settleExpense.getOriginBusinessType(),
                "summary", "接案介绍费", "incomeTypeId", "A09", "expenseDetailsTypeId", "002052","date",settleExpense.getCreateDate(),
                "paymentAmount", settleExpense.getAmount(), "expenseAmount", settleExpenseFee.getAmount(),
                "deptId", settleExpense.getDept().getId(), "companyId", settleExpense.getPayment().getId());
        createExpenseByFee(map);
    }


    @Override
    public Map<String, Object> getExpenseModel(Map map) {
        return null;
    }

    @Override
    public boolean support(String type) {
        return "SettleExpense".equals(type);
    }


}
